package com.open.api.service.impl;

import com.alibaba.fastjson.JSON;
import com.open.api.annotation.OpenApiService;
import com.open.api.bo.ImageBO;
import com.open.api.bo.MessageBO;
import com.open.api.config.property.FileProperty;
import com.open.api.config.property.FtpProperty;
import com.open.api.ftp.common.ResponseData;
import com.open.api.ftp.common.TypeEnum;
import com.open.api.ftp.util.FileIOUtil;
import com.open.api.ftp.util.FtpUtil;
import com.open.api.mapper.SendinfoDao;
import com.open.api.model.ResultModel;
import com.open.api.pojo.Sendinfo;
import com.open.api.service.MessageService;
import com.open.api.util.MyImageUtil;
import com.open.api.util.UUIDUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.util.Date;
import java.util.Map;

@Service
@OpenApiService
public class MessageServiceImpl implements MessageService {

    @Autowired
    private FtpProperty ftpProperty;
    @Autowired
    private FileProperty fileProperty;
    @Resource
    private SendinfoDao sendinfoDao;
    /**
     * 日志
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(MessageServiceImpl.class);

    @Override
    public ResultModel SendMessage(String appId, MessageBO messageBO) {
        LOGGER.info("【{}】>> 开放接口MessageService >> 方法SendMessage  params={}", appId, JSON.toJSONString(messageBO));
        ResultModel resultModel=new ResultModel();
        FileIOUtil fileIOUtil=new FileIOUtil();
        if(messageBO==null){
            resultModel.setSuccess(false);
            resultModel.setErrorCode("1");
            resultModel.setErrorMsg("对象不能为空!");
            return resultModel;
        }
        Sendinfo sendinfo=new Sendinfo();
        sendinfo.setId(String.valueOf(UUIDUtils.create()));
        sendinfo.setGatewayid(appId);
        sendinfo.setSendinfo(JSON.toJSONString(messageBO));
        sendinfo.setStatus("N");
        sendinfo.setCreatetime(new Date());

        int s=sendinfoDao.insertSelective(sendinfo);
        if(s>0){
            resultModel.setSuccess(true);
        }else{
            resultModel.setSuccess(false);
            resultModel.setErrorCode("4");
            resultModel.setErrorMsg("数据保存失败！");
        }
        return resultModel;
    }

    @Override
    public ResultModel GetMessageFromFtp(String requestId,String str) {

        LOGGER.info("【{}】>> 开放接口MessageService >> 方法GetMessageFromFtp  params={}", requestId);
        ResultModel resultModel=new ResultModel();
        FileIOUtil fileIOUtil=new FileIOUtil();
        FtpUtil ftpUtil=new FtpUtil();
        if(requestId.isEmpty()){
            resultModel.setSuccess(false);
            resultModel.setErrorCode("1");
            resultModel.setErrorMsg("requestId不能为空!");
            return resultModel;
        }

        //测试
        //ResponseData responseData=ftpUtil.defaultDownloadDir(ftpProperty.getHost(),ftpProperty.getPort(),ftpProperty.getUsername(),ftpProperty.getPassword(),ftpProperty.getBasepath(),ftpProperty.getSendpath(),fileProperty.getDownloaddir());
        //生产
        ResponseData responseData=ftpUtil.defaultDownloadDir(ftpProperty.getHost(),ftpProperty.getPort(),ftpProperty.getUsername(),ftpProperty.getPassword(),ftpProperty.getBasepath(),ftpProperty.getReceivepath(),fileProperty.getDownloaddir());
        if(responseData.getCode()==0){
            Map<String,Object> map=fileIOUtil.DirsAndFilesCount(fileProperty.getDownloaddir());
            //测试
            //ResponseData responseData_cleanFtpFile=ftpUtil.cleanFtp(map,ftpProperty.getHost(),ftpProperty.getPort(),ftpProperty.getUsername(),ftpProperty.getPassword(),ftpProperty.getBasepath(),ftpProperty.getSendpath(),fileProperty.getDownloaddir());
            //生产
            ResponseData responseData_cleanFtpFile=ftpUtil.cleanFtp(map,ftpProperty.getHost(),ftpProperty.getPort(),ftpProperty.getUsername(),ftpProperty.getPassword(),ftpProperty.getBasepath(),ftpProperty.getReceivepath(),fileProperty.getDownloaddir());
            if(responseData_cleanFtpFile.getCode()==0){
                resultModel.setSuccess(true);
                resultModel.setData(map);
            }else{
                resultModel.setSuccess(false);
                resultModel.setErrorCode(String.valueOf(responseData_cleanFtpFile.getCode()));
                resultModel.setErrorMsg(responseData_cleanFtpFile.getMsg());
                return resultModel;
            }
        }else{
            resultModel.setSuccess(false);
            resultModel.setErrorCode(String.valueOf(responseData.getCode()));
            resultModel.setErrorMsg(responseData.getMsg());
        }
        return resultModel;
    }

    @Override
    public ResultModel ReadFile(String requestId, String fileName) {

        LOGGER.info("【{}】>> 开放接口MessageService >> 方法ReadFile  params={}", requestId, fileName);
        ResultModel resultModel=new ResultModel();
        FileIOUtil fileIOUtil=new FileIOUtil();
        FtpUtil ftpUtil=new FtpUtil();
        if(fileName.isEmpty() || requestId.isEmpty()){
            resultModel.setSuccess(false);
            resultModel.setErrorCode("1");
            resultModel.setErrorMsg("参数requestId和fileName不能为空");
            return resultModel;
        }

        resultModel=fileIOUtil.getDatafromFile(fileName,fileProperty.getDownloaddir());
        return resultModel;
    }

    @Override
    public ResultModel DeleteTempFile(String requestId, String fileName) {
        LOGGER.info("【{}】>> 开放接口MessageService >> 方法DeleteTempFile  params={}", requestId, fileName);
        ResultModel resultModel=new ResultModel();
        FileIOUtil fileIOUtil=new FileIOUtil();
        if(fileName.isEmpty() || requestId.isEmpty()){
            resultModel.setSuccess(false);
            resultModel.setErrorCode("1");
            resultModel.setErrorMsg("参数requestId和fileName不能为空");
            return resultModel;
        }
        String filePath=fileProperty.getDownloaddir()+fileName;
        ResponseData responseData=fileIOUtil.deleteFile(filePath);
        if(responseData.getCode()==0){
            resultModel.setSuccess(true);
        }else{
            resultModel.setSuccess(false);
            resultModel.setErrorCode(String.valueOf(responseData.getCode()));
            resultModel.setErrorMsg(responseData.getMsg());
        }
        return resultModel;
    }
/*

    @Override
    public ResultModel SendImage(String requestId, ImageBO imageBO) {
        LOGGER.info("【{}】>> 开放接口MessageService >> 方法SendImage  params={}", requestId, imageBO.toString());
        ResultModel resultModel=new ResultModel();
        FileIOUtil fileIOUtil=new FileIOUtil();
        if(imageBO==null){
            resultModel.setSuccess(false);
            resultModel.setErrorCode("1");
            resultModel.setErrorMsg("图片名称不能为空!");
            return resultModel;
        }

        ResultModel resultModel_saveFile=fileIOUtil.saveImage(imageBO.getImagename(),fileProperty.getUploaddir(),imageBO.getImagestr());
        if(!resultModel_saveFile.isSuccess()){
            return resultModel_saveFile;
        }
        FtpUtil ftpUtil=new FtpUtil();
        String localTmpSendPath=fileProperty.getUploaddir()+imageBO.getImagename();
        ResponseData responseData =ftpUtil.defaultUpload(ftpProperty.getHost(),ftpProperty.getPort(),ftpProperty.getUsername(),ftpProperty.getPassword(),ftpProperty.getBasepath(),ftpProperty.getSendpath(),imageBO.getImagename(),new File(localTmpSendPath));
        LOGGER.info(responseData.toString());
        if(responseData.getCode()==0){
            System.out.println("上传ftp成功");
            ResponseData responseData_delFile=fileIOUtil.deleteFromServer(localTmpSendPath, TypeEnum.DELETETYPE_RMFILE.getCode());
            if(responseData_delFile.getCode()==0){
                resultModel.setSuccess(true);
            }else{
                resultModel.setSuccess(false);
                resultModel.setErrorCode(String.valueOf(responseData_delFile.getCode()));
                resultModel.setErrorMsg(resultModel_saveFile.getErrorMsg());
                return resultModel;
            }
        }else {
            resultModel.setSuccess(false);
            resultModel.setErrorCode(String.valueOf(responseData.getCode()));
            resultModel.setErrorMsg(responseData.getMsg());
        }
        return resultModel;
    }
*/

    @Override
    public ResultModel ReadImage(String requestId, ImageBO imageBO) {
        LOGGER.info("【{}】>> 开放接口MessageService >> 方法ReadImage  params={}", requestId, imageBO.toString());
        ResultModel resultModel=new ResultModel();
        resultModel=MyImageUtil.GetImageStr(fileProperty.getDownloaddir()+imageBO.getImagepath());
        return resultModel;
    }

    @Override
    public ResultModel SendImage(String requestId, ImageBO imageBO) {
        LOGGER.info("【{}】>> 开放接口MessageService >> 方法SendImage  params={}", requestId, imageBO.toString());
        ResultModel resultModel=new ResultModel();
        FileIOUtil fileIOUtil=new FileIOUtil();
        ResultModel resultModel_saveFile=fileIOUtil.saveImageToFile(imageBO,fileProperty.getUploadimagedir(),fileProperty.getSuffix());
        String newFileName="";
        if(!resultModel_saveFile.isSuccess()){
            return resultModel_saveFile;
        }else{
            newFileName= (String) resultModel_saveFile.getData();
        }

        FtpUtil ftpUtil=new FtpUtil();
        String localTmpSendPath=fileProperty.getUploadimagedir()+imageBO.getSystem()+"/"+newFileName;
        ResponseData responseData =ftpUtil.defaultUpload(ftpProperty.getHost(),ftpProperty.getPort(),ftpProperty.getUsername(),ftpProperty.getPassword(),ftpProperty.getBasepath(),ftpProperty.getUpimagepath(),newFileName,new File(localTmpSendPath));
        LOGGER.info(responseData.toString());
        if(responseData.getCode()==0){
            System.out.println("上传ftp成功");
            ResponseData responseData_delFile=fileIOUtil.deleteFromServer(localTmpSendPath, TypeEnum.DELETETYPE_RMFILE.getCode());
            if(responseData_delFile.getCode()==0){
                resultModel.setSuccess(true);
            }else{
                resultModel.setSuccess(false);
                resultModel.setErrorCode(String.valueOf(responseData_delFile.getCode()));
                resultModel.setErrorMsg(resultModel_saveFile.getErrorMsg());
                return resultModel;
            }
        }else {
            resultModel.setSuccess(false);
            resultModel.setErrorCode(String.valueOf(responseData.getCode()));
            resultModel.setErrorMsg(responseData.getMsg());
        }
        return resultModel;
    }
    public static void main(String[] args) {
        MessageBO messageBO=new MessageBO();
       /* messageBO.setSystem("epms");
        messageBO.setAction("DELETE");
        messageBO.setActionurl("/place/csxx/delete/{id}");
        messageBO.setActiondesc("场所基本信息备案_场所信息删除");
        messageBO.setData("中电科");*/
        messageBO.setFilename("2020-05-16_158961059455355832569");
       /* messageBO.setMd5("cc5a2db2ab0219fd8a6cf2b6e7e6344b");
        messageBO.setResource("place");*/

        System.out.println(JSON.toJSONString(messageBO));
    }
}
